package com.cn.camundaappliction.controller;

import org.camunda.bpm.engine.HistoryService;
import org.camunda.bpm.engine.RepositoryService;
import org.camunda.bpm.engine.RuntimeService;
import org.camunda.bpm.engine.TaskService;
import org.camunda.bpm.engine.history.HistoricProcessInstance;
import org.camunda.bpm.engine.repository.ProcessDefinition;
import org.camunda.bpm.engine.runtime.ProcessInstance;
import org.camunda.bpm.engine.task.Task;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * 任务api的方式
 */
@RestController
public class TaskController {

    @Autowired
    private RepositoryService repositoryService;


    @Autowired
    private TaskService taskService;

    @Autowired
    private RuntimeService runtimeService;

    @Autowired
    private HistoryService historyService;

    @GetMapping("/taskQuery/{name}")
    public void taskQuery(@PathVariable("name") String name){
        List<ProcessDefinition> list = repositoryService.createProcessDefinitionQuery().list();
        System.out.println("查询任务api的方式"+list);
        //查询根据名称查询张三的api的方式
        List<Task> tasks = taskService.createTaskQuery().taskAssignee("张三").list();
        System.out.println("任务查询的"+tasks.toString());
        //分页查询方式
        List<ProcessInstance> instances = runtimeService.createProcessInstanceQuery().listPage(1, 10);
        System.out.println("分页查询的数据信息"+instances.toString());
    }

    /**
     * 查询历史的任务
     */
    @GetMapping("/taskList")
    public void taskList(){
        List<HistoricProcessInstance> list = historyService.createHistoricProcessInstanceQuery().list();
        System.out.println("查询历史的方式"+list);
    }

    /**
     * 查询当前任务分页的方式
     */
    @GetMapping("/taskPage")
    public void taskPage(){
        List<Task> list = taskService.createTaskQuery().orderByTaskCreateTime().desc().list();
        System.out.println("查询当前任务"+list.toString());
    }


}
